#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "adc.h"
#include "extern_16ch_adc.h"
#include "../menu/Menu.h"

extern volatile struct str_menu_data menu_data;
void Init_adc(void)
 {
 	ADCSRA|=1<<ADEN|1<<ADPS2|1<<ADPS1|1<<ADPS0;  
	Init_16ch_adc();
 };

void go_sleep(void)
{
   	On_interrupt_adc;
     sleep_enable();
	 sleep_cpu();
	 sleep_disable();
	Off_interrupt_adc;
}


int read_adc(unsigned char adc_mux)
 {
  
    ADMUX=adc_mux;
	ADCSRA |= 1<<ADSC;

	while (!(ADCSRA & (1<<ADIF)));	
	ADCSRA |= 1<<ADIF;

	return ADCW&0x3FF;;
 };



int get_adc(unsigned char ext_int,unsigned char num)
{
 if (ext_int)
  {
  	Init_16ch_adc();
	return ext_read_adc(num);
  } else
  {
    return read_adc(num);
  }
};

SIGNAL(SIG_ADC)
 {  
  adc_signal=ADCW&0x3FF;
 };

void read_adcs (void)
{
 menu_data.power_lazer1 = get_adc(0,LASP1);
 menu_data.power_lazer2 = get_adc(0,LASP2);
 menu_data.current_lazer1 = get_adc(0,LASC1);
 menu_data.current_lazer2 = get_adc(0,LASC2);
}
